const Router = require("express").Router(); // 配置后端路由
const Mysql = require("../../mysql/mysql.js"); // 引入数据库
const { token } = require("../../token/token.js");
const formidable = require("formidable");
const path = require("path");
let fs = require("fs");
// 登陆
Router.post("/login", async (request, response) => {
  let { username, password } = request.body;
  // 查询用户是否存在
  // 用户密码是否正确
  // 用户是否为超级用户
  // 能访问的信息
  //
  let sql = `SELECT * FROM user_id WHERE user="${username}" AND password="${password}"`;
  let userData = await Mysql(sql);
  if (userData.length > 0) {
    // 携带一个权限
    let visitSql = `SELECT * FROM roles WHERE id="${userData[0].auth}"`;
    let visitList = await Mysql(visitSql);
    response.json({
      code: 2000,
      msg: "登陆成功",
      token: token(request.body),
      auth: userData[0].auth,
      visit: userData[0].visit,
      visitList,
    });
  } else {
    response.json({
      code: 2004,
      msg: "用户名或密码错误",
    });
  }
});
//商品列表
Router.post("/commodity", (request, response) => {
  Mysql(`SELECT * FROM shop_list`).then((res) => {
    if (res.length > 0) {
      response.json({
        code: 2000,
        msg: "获取成功",
        data: res,
      });
    } else {
      response.json({
        code: 2004,
        msg: "请求失败",
      });
    }
  });
});
// 上传图片、视频、音乐、文件
Router.post("/upload", (request, response) => {
  var form = new formidable.IncomingForm();
  form.keepExtensions = true;
  form.multiples = true;
  form.uploadDir = path.join(__dirname, "../../public/store");
  form.parse(request, function (err, fields, files) {
    response.end(JSON.stringify({ fields, files }));
  });
});
// 初步获取数据库图片
Router.post("/navicon", (request, response) => {
  Mysql(`SELECT * FROM nav_img`).then((res) => {
    if (res.length > 0) {
      response.json({
        code: 2000,
        msg: "获取成功",
        data: res,
      });
    } else {
      response.json({
        code: 2004,
        msg: "请求失败",
      });
    }
  });
});
// 点击更换图标
Router.post("/change", (request, response) => {
  var form = new formidable.IncomingForm();
  form.keepExtensions = true;
  form.multiples = true;
  form.uploadDir = path.join(__dirname, "../../public/home");
  form.parse(request, function (err, fields, files) {
    console.log(files);
    // 查询字符最后的位置
    let wei = files.a.path.lastIndexOf("u");
    // 截取某段字符串
    let pathImg = files.a.path.substring(wei, files.a.path.length);
    let url = "http://localhost:8888/home/" + pathImg;
    Mysql(`UPDATE  nav_img  SET icon='${url}' WHERE name='秒杀'`).then(
      (res) => {
        if (res.length > 0) {
          response.json({
            code: 2000,
            msg: "获取成功",
            data: res,
          });
        } else {
          response.json({
            code: 2004,
            msg: "请求失败",
          });
        }
      }
    );
  });
});
// 药品搜索

Router.post("/search", (request, response) => {
  const { value } = request.body;
  Mysql(`select * from shop_list where name like "%${value}%"`).then((res) => {
    if (res.length > 0) {
      response.json({
        code: 2000,
        msg: "搜索成功",
        data: res,
      });
    } else {
      response.json({
        code: 2004,
        msg: "没有该数据",
      });
    }
  });
});

module.exports = Router;
